package net.qhzw.vip.qiyu.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.DtsUser;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.MemberWalletService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("member/wallet")
@Api(tags = "用户钱包管理")
@Slf4j
public class MemberWalletController {

    @Autowired
    private MemberWalletService memberWalletService;

    @ApiOperation(value = "表格导入充值")
    @PostMapping("importRecharge")
    public JsonResult importRecharge(@RequestParam("file") MultipartFile file) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        if (shopId == 1) {
            throw new ApiException("您不能充值，请登录商户账号进行操作！");
        }
        List<String> result = memberWalletService.importRecharge(file, shopId);
        return JsonResult.ok(result);
    }

    @ApiOperation(value = "系统充值")
    @PostMapping("systemRecharge")
    public JsonResult systemRecharge(@RequestBody Map<String, String> param) {
        Long shopId = SecurityUtils.getLoginUser().getShopId();
        if (shopId == 1) {
            throw new ApiException("您不能充值，请登录商户账号进行操作！");
        }
        Long userId = Long.valueOf(param.get("userId"));
        Byte status = Byte.valueOf(param.get("status"));
        BigDecimal amount = new BigDecimal(param.get("amount"));
        String remark = param.get("remark");
        boolean isOK = memberWalletService.systemRecharge(userId, amount, status, remark, shopId);
        if (isOK) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "充值模版下载")
    @GetMapping("download")
    public void rechargeTemDownload(HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, null), DtsUser.class, new ArrayList<>());
        ServletOutputStream os = null;
        try {
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("表格导入充值.xls", "UTF-8"));
            os = response.getOutputStream();
            workbook.write(os);
            os.close();
            workbook.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error("充值模版下载ERROR,msg{} : ", e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


}
